<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">


    <div class="content-header">
        <h1>用户管理<small>权限列表</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>权限列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>


    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <!--条件框的内容-->
                <el-input placeholder="权限名称/权限关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>

            <!--查询后显示的效果-->  <!--todo  已改-->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>

                <el-table-column prop="name" label="权限名称" align="center"></el-table-column>
                <el-table-column prop="keyword" label="权限关键字" align="center"></el-table-column>
                <el-table-column prop="description" label="权限描述" align="center"></el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>

            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        :total="pagination.total"
                        layout="total, prev, pager, next, jumper">
                </el-pagination>
            </div>

            <!--todo  已改-->
            <!--todo 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增权限" :visible.sync="dialogFormVisible">
                    <!-- ref  添加校验规则--> <!--新添加的所有数据 都会存到 formData 里面，提交直接提交这个 统一提交-->
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限关键字" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <!--提交数据的功能  使用 handleAdd() 方法 -->
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--todo  已改-->
            <!--todo 修改标签弹层 -->
            <div class="add-form">
                <el-dialog title="修改权限" :visible.sync="dialogFormVisible4Edit">
                    <!-- ref  添加校验规则--> <!--新添加的所有数据 都会存到 formData 里面，提交直接提交这个 统一提交-->
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限关键字" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <!--提交数据的功能  使用 handleEdit() 方法 -->
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 500,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '权限名称为必填项', trigger: 'blur'}],
                keyword: [{required: true, message: '权限关键字为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },

        methods: {
            //编辑
            handleEdit() {
                //权限校验
                this.$refs['dataEditForm'].validate((vali) => {
                    if (vali == true) {
                        //表单检验通过，发送ajax请求
                        //首先关闭窗口
                        this.dialogFormVisible4Edit = false;
                        axios.post("/permission/update.do", this.formData).then((req) => {
                            //判断返回的数据状态
                            if (req.data.flag) {

                                this.$message.success(req.data.message)
                            } else {
                                this.$message.error(req.data.message)
                            }

                        }).catch((response) => {
                            this.showMessage(response)   //权限不足的提示信息
                        }).finally(() => {
                            //成功查询一次
                            this.findPage();
                        })
                    } else {
                        this.$message.error("表单检验不通过，请检查后重试")
                    }
                })
            },

            //添加
            handleAdd() {
                //权限校验
                this.$refs['dataAddForm'].validate((vali) => {
                    if (vali == true) {

                        //点击确定后，你不管成功还是失败都要关闭添加的窗口
                        this.dialogFormVisible = false;

                        //发送ajax请求
                        axios.post("/permission/add.do", this.formData).then((res) => {
                            //判断后台传递过来的值
                            if (res.data.flag) {
                                //给前端的数据赋值
                                this.formData = res.data.data
                                this.$message.success(res.data.message)
                            } else {
                                this.$message.error(res.data.message)
                            }
                        }).catch((req) => {
                            this.showMessage(req)   //权限不足的错误
                        }).finally(() => {
                            //在添加完毕后。无论成功还是失败，都要重新查询数据
                            this.findPage();
                        })
                    } else {
                        this.$message.error("表单校验不通过，请检查后重试")
                    }
                });
            },

            //分页查询   使用了钩子函数  页面加载完毕后自动执行一次
            findPage() {
                //定义数据，查询数据库
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }

                //发送ajax请求
                axios.post("/permission/findPage.do", param).then((response) => {
                    //响应回来的数据
                    this.pagination.total = response.data.total    //总条数
                    this.dataList = response.data.rows    //遍历的数据
                })

            },

            // 重置表单
            resetForm() {
                this.formData = {}
            },

            // 弹出添加窗口
            handleCreate() {
                //弹出窗口前重置原来的数据
                this.resetForm()
                this.dialogFormVisible = true;
            },

            //弹出编辑窗口
            handleUpdate(row) {   //这个row有这一行的所有的数据
                //弹出前清除数据
                this.resetForm()
                this.dialogFormVisible4Edit = true

                //查询数据库，查询出编辑的这个权限的详细信息用于展示
                axios.get("/permission/findById.do?id=" + row.id).then((res) => {
                    //判断状态
                    if (res.data.flag) {
                        //给窗口赋值
                        this.formData = res.data.data
                    } else {
                        this.$message.error(res.data.message)
                    }
                })
            },

            //切换页码  参数 currentPage 是切换后的页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            //权限不足框架抛出的异常信息
            showMessage(r) {
                if (r == "Error: Request failed with status code 403") {
                    //权限不足    
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },


            // 删除
            handleDelete(row) {
                //删除属于危险操作，必须提示
                this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                  axios.post("/permission/delete.do?id="+row.id).then((response) => {
                      if (response.data.flag){
                          this.$message.success(response.data.message)
                      }else{
                          this.$message.error(response.data.message)
                      }
                  }).catch((r) => {
                      this.showMessage(r)   //权限不足的提示
                  }).finally(() => {
                      this.findPage();
                  })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }

    })
</script>
</html>